﻿Public Class PRENAME

    Dim connect As CONDB = CONDB.NewConnection()
    Dim PROVIDER_MASPRENAME As New PROVIDERSCLASS

    Private Sub PRENAME_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SelectDatagridprename()

        DesignForm(Me)
        DesignGrid(MASPRENAMEGrid)

        PROVIDER_MASPRENAME.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Private Sub SelectDatagridprename()
        Dim selectcombobox As New masprename
        selectcombobox.loadcombobox(SEXComboBox)
        SEXComboBox.SelectedValue = -1

        Dim objSelectDatamasprename As New masprename
        Dim MASPRENAME As DataTable
        MASPRENAME = connect.GetTable(objSelectDatamasprename.selectMasprename_)
        MASPRENAMEGrid.PrimaryGrid.DataSource = MASPRENAME
    End Sub

    Private Sub MASPRENAMEGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles MASPRENAMEGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสคำนำหน้า").Value() IsNot DBNull.Value Then
            PRENAMEIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสคำนำหน้า").Value()
        Else
            PRENAMEIDTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("คำนำหน้าไทย(ย่อ)").Value() IsNot DBNull.Value Then
            STPRENAMETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("คำนำหน้าไทย(ย่อ)").Value()
        Else
            STPRENAMETxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("คำนำหน้าอังกฤษ(ย่อ)").Value() IsNot DBNull.Value Then
            SEPRENAMETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("คำนำหน้าอังกฤษ(ย่อ)").Value()
        Else
            SEPRENAMETxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("คำนำหน้าไทย(เต็ม)").Value() IsNot DBNull.Value Then
            FTPRENAMETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("คำนำหน้าไทย(เต็ม)").Value()
        Else
            FTPRENAMETxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("คำนำหน้าอังกฤษ(เต็ม)").Value() IsNot DBNull.Value Then
            FEPRENAMETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("คำนำหน้าอังกฤษ(เต็ม)").Value()
        Else
            FEPRENAMETxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("SEX").Value() IsNot DBNull.Value Then
            SEXComboBox.SelectedValue = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("SEX").Value()
        Else
            SEXComboBox.SelectedValue = -1
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            prenameStatusCheckbox.Checked = True
        Else
            prenameStatusCheckbox.Checked = False
        End If
        connect = CONDB.NewConnection()
        If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
            Try
                Dim Sql As String
                connect.BeginTrans()
                Sql = "UPDATE masprename SET "
                If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
                    Sql += "`status` = 1 "
                Else
                    Sql += "`status` = 0 "
                End If
                Sql += "WHERE `prename` = '" & PRENAMEIDTxt.Text & "' ;"
                connect.ExecuteNonQuery(Sql)
                connect.CommitTrans()
            Catch ex As Exception
                connect.RollbackTrans()
                MsgBox(ex.ToString)
            End Try
        End If
        connect.Dispose()
        'connectlocal = conlocal.NewConnection()
        'If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
        '    Try
        '        Dim Sql As String
        '        connectlocal.BeginTrans()
        '        Sql = "UPDATE MASPRENAME SET "
        '        If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
        '            Sql += "[STATUS] = 1 "
        '        Else
        '            Sql += "[STATUS] = 0 "
        '        End If
        '        Sql += "WHERE [PRENAME] = '" & PRENAMEIDTxt.Text & "' ;"

        '        connectlocal.ExecuteNonQuery(Sql)
        '        connectlocal.CommitTrans()
        '    Catch ex As Exception
        '        connectlocal.RollbackTrans()
        '        MsgBox(ex.ToString)
        '    End Try
        'End If
        'connectlocal.Dispose()
        addPrename.Enabled = False
    End Sub

    Private Sub addPrename_Click_1(sender As Object, e As EventArgs) Handles addPrename.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_MASPRENAME.PADD_ = True Then
                Dim objInsertData As New masprename
                objInsertData.STPRENAME_ = STPRENAMETxt.Text
                objInsertData.SEPRENAME_ = SEPRENAMETxt.Text
                objInsertData.FTPRENAME_ = FTPRENAMETxt.Text
                objInsertData.FEPRENAME_ = FEPRENAMETxt.Text
                objInsertData.SEX_ = SEXComboBox.SelectedValue
                objInsertData.prenameStatus_ = prenameStatusCheckbox.Checked
                objInsertData.insertMasprenameData()

                SelectDatagridprename()

                FTPRENAMETxt.Text = ""
                STPRENAMETxt.Text = ""
                FEPRENAMETxt.Text = ""
                SEPRENAMETxt.Text = ""
                prenameStatusCheckbox.Checked = False
                SEXComboBox.SelectedValue = -1
            Else
                MsgBox(PROVIDER_MASPRENAME.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editPrename_Click_1(sender As Object, e As EventArgs) Handles editPrename.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_MASPRENAME.PUPD_ = True Then
                Dim editStat As Integer
                editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลคำนำหน้าชื่อดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

                If editStat = 1 Then
                    Dim objEditData As New masprename
                    objEditData.prenameID_ = PRENAMEIDTxt.Text
                    objEditData.STPRENAME_ = STPRENAMETxt.Text
                    objEditData.SEPRENAME_ = SEPRENAMETxt.Text
                    objEditData.FTPRENAME_ = FTPRENAMETxt.Text
                    objEditData.FEPRENAME_ = FEPRENAMETxt.Text
                    objEditData.SEX_ = SEXComboBox.SelectedValue
                    objEditData.prenameStatus_ = prenameStatusCheckbox.Checked
                    objEditData.editMasprenameData()

                    SelectDatagridprename()

                    FTPRENAMETxt.Text = ""
                    STPRENAMETxt.Text = ""
                    FEPRENAMETxt.Text = ""
                    SEPRENAMETxt.Text = ""
                    prenameStatusCheckbox.Checked = False
                    SEXComboBox.SelectedValue = -1
                End If
                addPrename.Enabled = True
            Else
                MsgBox(PROVIDER_MASPRENAME.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL(masprename.Text)
        nextform.ShowDialog()
    End Sub

    Private Sub ButtonItem1_Click(sender As Object, e As EventArgs) Handles ButtonItem1.Click
        Dim nextform As New IMPORTEXCEL(masprename.Text)
        nextform.ShowDialog()
    End Sub
End Class